package com.jvyou.house.controller;

import com.jvyou.common.annotation.Log;
import com.jvyou.common.core.controller.BaseController;
import com.jvyou.common.core.domain.AjaxResult;
import com.jvyou.common.core.page.TableDataInfo;
import com.jvyou.common.enums.BusinessType;
import com.jvyou.common.utils.poi.ExcelUtil;
import com.jvyou.house.domain.House;
import com.jvyou.house.domain.HouseImage;
import com.jvyou.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 房源Controller
 *
 * @author Jvyou
 * @date 2024-09-16
 */
@RestController
@RequestMapping("/house/house")
public class HouseController extends BaseController {
    @Autowired
    private IHouseService houseService;


    @GetMapping("/images/{id}")
    public AjaxResult getImages(@PathVariable Long id) {
        return AjaxResult.success("成功", houseService.getImages(id));
    }

    @PostMapping("/add-images/{id}")
    public AjaxResult saveImage(@PathVariable("id") Long id,@RequestBody List<HouseImage> houseImageus) {
        return houseService.saveImage(id, houseImageus);
    }

    /**
     * 查询房源列表
     */
    @GetMapping("/list")
    public TableDataInfo list(House house) {
        startPage();
        List<House> list = houseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 导出房源列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:export')")
    @Log(title = "房源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, House house) {
        List<House> list = houseService.selectHouseList(house);
        ExcelUtil<House> util = new ExcelUtil<House>(House.class);
        util.exportExcel(response, list, "房源数据");
    }

    /**
     * 获取房源详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(houseService.selectHouseById(id));
    }

    /**
     * 新增房源
     */
    @PreAuthorize("@ss.hasPermi('house:house:add')")
    @Log(title = "房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody House house) {
        return toAjax(houseService.insertHouse(house));
    }

    /**
     * 修改房源
     */
    @Log(title = "房源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody House house) {
        return toAjax(houseService.updateHouse(house));
    }

    /**
     * 删除房源
     */
    @PreAuthorize("@ss.hasPermi('house:house:remove')")
    @Log(title = "房源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(houseService.deleteHouseByIds(ids));
    }
}
